package com.inspur.dgov.bsp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inspur.dgov.bsp.user.entity.PubRegion;
import com.inspur.dgov.bsp.user.entity.PubRegionRelation;
import com.inspur.dgov.bsp.user.mapper.PubRegionMapper;
import com.inspur.dgov.bsp.user.service.PubRegionRelationService;
import com.inspur.dgov.bsp.user.service.PubRegionService;
import com.inspur.dgov.bsp.user.vo.RegionVO;
import com.inspur.dgov.bsp.user.vo.RegionWebVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 区划信息 服务实现类
 *
 * @author chiyupei
 * @version 1.0
 * @since 1.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class PubRegionServiceImpl extends ServiceImpl<PubRegionMapper, PubRegion> implements PubRegionService {

    @Autowired
    private PubRegionRelationService pubRegionRelationService;

    /**
     * 区划分页数据
     * @param page
     * @param params
     * @return
     */
    @Override
    public IPage<RegionVO> selectRegionPage(Page page, Map<String, Object> params) {
        return baseMapper.selectRegionPage(page, params);
    }

    /**
     * 添加区划
     * @param region
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveRegion(PubRegion region) {
        boolean rs = super.save(region);
        //添加区划关系
        Long id = region.getId();
        Long parentId = region.getParentId();
        //添加自身关系
        PubRegionRelation own = new PubRegionRelation();
        own.setAncestorId(id);
        own.setDescendantId(id);
        own.setDistance(0);
        this.pubRegionRelationService.save(own);
        //添加递归关系
        if (null != parentId) {
            PubRegionRelation relation = new PubRegionRelation();
            relation.setAncestorId(id);
            relation.setDescendantId(parentId);
            this.pubRegionRelationService.insertRecursionRegionRelation(relation);
        }
        return rs;
    }

    /**
     * 移动区划
     * @param region
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moveRegion(PubRegion region) {
        PubRegionRelation relation = new PubRegionRelation();
        relation.setAncestorId(region.getId());
        relation.setDescendantId(region.getParentId());
        //分离子树
        this.pubRegionRelationService.separateRegionRelation(relation);
        //移动子树
        this.pubRegionRelationService.moveRegionRelation(relation);
    }

    /**
     * 删除区划
     * @param region
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRegion(PubRegion region) {
        Long regionId = region.getId();
        //删除区划关系
        UpdateWrapper<PubRegionRelation> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(PubRegionRelation::getAncestorId, regionId)
                .or().eq(PubRegionRelation::getDescendantId, regionId);
        this.pubRegionRelationService.remove(wrapper);
        return super.removeById(regionId);
    }

    /**
     * 区划接口服务
     * @param region
     * @return
     */
    @Override
    public List<RegionWebVO> webRegion(PubRegion region) {
        return baseMapper.webRegion(region);
    }
}
